package cn.hpclub.server.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.util.InterfaceUtil;

public class Banner extends Model<Banner> implements Serializable{

    private static final long  serialVersionUID = -89555379613217315L;

    public static final Banner dao              = new Banner();

    /**
     * @Title: findBannerByPage
     * @Description:根据关键字分页查询banner
     * @param property
     * @param keyWord
     * @param pageNumber
     * @param pageSize
     * @return Page<Banner>
     */
    public Page<Banner> findBannerByPage(String property, String keyWord, int pageNumber, int pageSize){
        List<Object> paras = new ArrayList<Object>();

        String select = " select *, (select org.DisplayName from organization org where org.id = b.org_id) as org_name ";
        String sqlExceptSelect = " from banner as b where b.is_deleted = ? ";
        paras.add(0);

        sqlExceptSelect += " and b.org_id in("
                + InterfaceUtil.getChlidOrgIdSql(InterfaceUtil.getOrgId(), Constant.MAX_ORG_LEVEL, true) + ") ";

        if(StrKit.notBlank(property) && StrKit.notBlank(keyWord)){
            sqlExceptSelect += " and " + property + " like '%" + keyWord + "%' ";
        }
        sqlExceptSelect += " order by sort asc ";
        Page<Banner> pager = dao.paginate(pageNumber, pageSize, select, sqlExceptSelect, paras.toArray());

        return pager;
    }

    public boolean deleteBannerByID(String Ids){
        return Banner.dao.deleteById(Ids);
    }

    /**
     * @Title: getBannerList
     * @Description:根据机构ID查询广告列表（暂不限制机构ID，查询所有广告）
     * @param orgID
     *            机构ID
     * @return List<Banner> 广告列表
     */
    public List<Banner> getBannerList(int orgID,int serverType){

        String sqlExceptSelect = "";
        String orgSql = InterfaceUtil.getParentOrgIdSql(orgID, Constant.MAX_ORG_LEVEL);
        if(StrKit.notBlank(orgSql)){
            sqlExceptSelect += " and org_id in " + orgSql;
        }

        String sql = "select * from banner where enabled=1 and is_deleted=0" ;
        if(serverType>0){
            sql = sql+ " AND server_type="+serverType;
        }
        sql = sql+ sqlExceptSelect + " ORDER BY sort asc ";
        List<Banner> list = dao.find(sql);
        return list;
    }

}
